/*
 * File Name:mal_led.c
 * Version:v1.0.1
 * Change Logs:
 * Date             Author          main change
 * 2023-11-17       lanxin          the first version
 * 2024-03-01       lanxin          recode
 * 2025-01-18       lanxin          update comment,delete mal_led_set_state_with_delay
 *
*/

/********************************************* READ ME BEGIN *************************************************************/

/* Brief:
 *
 * *1.led 中间层实现。
 *
 */

/* Notice:
 *
 * *1.调用 mal_led_init() 前应调用 mal_led_bind_ops 进行操作函数的指定。
 *
 * *2.操作时，如果未进行初始化，则会在该操作时进行初始化。
 *
 * *3.操作前，可以调用 mal_led_get_ready 判断下是否初始化完成。
 * 
 */

/********************************************* READ ME END ***************************************************************/

/* source file */
#include "mal_led.h"

/* private define */

/* variable typedef */

/* function declaration */

/* variable declaration */
static s_led_ops_t *_led_ops = 0;
static int _led_init_state = 0;

static void _mal_led_check_init ( void )
{
	if ( 0 == _led_init_state )
		mal_led_init();
}

void mal_led_bind_ops ( s_led_ops_t *ops )
{
	if ( ops )
		_led_ops = ops;
}

int mal_led_init ( void )
{
	if ( _led_ops && _led_ops->init && ( 0 == _led_init_state ) )
	{
		_led_init_state = 1;

		return _led_ops->init();
	}

	return -1;
}

int mal_led_set_state ( uint8_t index, uint8_t state )
{
	_mal_led_check_init();

	if ( _led_ops && _led_ops->set_state && _led_init_state )
		return _led_ops->set_state ( index, state );

	return -1;
}

int mal_led_get_ready ( void )
{
	return _led_init_state;
}
